<?php
class Wow_Armory_Handler_Default extends Wow_Armory_Core_Handler_Abstract
{
    
    const TWOONTWO = '2on2';
    const THREEONTHREE = '3on3';
    const FIVEONFIVE = '5on5';
    
    public function __construct()
    {
        Wow_Armory_Parse_Result_Need::set(self::CHARACTER);
    }
    
    public function parse()
    {
        $result = Wow_Armory_Parse_Result_Registry::get(self::CHARACTER);
        
        $characterInfo = $result['characterInfo'];
        
        $global    = (array) $characterInfo;
        $character = (array) $global['character'];
        $global    = (array) $character['@attributes'];
        $this->_toResults($global);
        
        $arena = (array) $character['arenaTeams'];
        $arena = (array) $arena['arenaTeam'];
        
        if (count($arena) > 0) {
            $this->_parseArena($arena);
        }
        
        return $this;
    }
    
    /**
     * Returns the arena team size as a string.
     * 
     * @param string $size
     * @return string
     */
    protected function _parseArenaTeamSize($size)
    {
        switch ($size) {
                    
            case '2':
                return self::TWOONTWO;
                break;
                
            case '3':
                return self::THREEONTHREE;
                break;
                
            case '5':
                return self::FIVEONFIVE;
                break;
            
        }
    }
    
    protected function _parseArena(Array $teams)
    {
        if (isset($teams['@attributes'])) {
            $teamSize = $this->_parseArenaTeamSize(
                $teams['@attributes']['teamSize']
            );
            $this->_results['arenaTeams'][$teamSize] = 
                    new Wow_Armory_Handler_Element_Default_Arena_Team($teams['@attributes']);
        } else {
            foreach ($teams as $team) {
                $teamSize = $this->_parseArenaTeamSize($team['teamSize']);
                $array   = (array) $team;
                $emblem  = (array) $array['emblem'];
                $emblem  = $emblem['@attributes'];
                $members = (array) $array['members'];
                $data    = $array['@attributes'];
                $this->_results['arenaTeams'][$teamSize] = 
                    new Wow_Armory_Handler_Element_Default_Arena_Team(
                        $data,
                        new Wow_Armory_Handler_Element_Default_Arena_Team_Emblem($emblem),
                        $members
                    );
            }
        }
    }
    
    /**
     * Returns the battle group.
     * 
     * @return string
     */
    public function getBattleGroup()
    {
        return $this->battleGroup;
    }
    
    /**
     * Returns the char url.
     * 
     * @return string
     */
    public function getCharUrl()
    {
        return $this->charUrl;
    }
    
    /**
     * Returns the class name.
     * 
     * @return string
     */
    public function getClass()
    {
        return $this->class;
    }
    
    /**
     * Returns the class id.
     * 
     * @return string
     */
    public function getClassId()
    {
        return $this->classId;
    }
    
    /**
     * Returns the class url.
     * 
     * @return string
     */
    public function getClassUrl()
    {
        return $this->classUrl;
    }
    
    /**
     * Returns the faction.
     * 
     * @return string
     */
    public function getFaction()
    {
        return $this->faction;
    }
    
    /**
     * Returns the faction ids.
     * 
     * @return string
     */
    public function getFactionId()
    {
        return $this->factionId;
    }
    
    /**
     * Returns the gender.
     * 
     * @return string
     */
    public function getGender()
    {
        return $this->gender;
    }
    
    /**
     * Returns the gender id.
     * 
     * @return string
     */
    public function getGenderId()
    {
        return $this->genderId;
    }
    
    /**
     * Returns the guild name.
     * 
     * @return string
     */
    public function getGuildName()
    {
        return $this->guildName;
    }
    
    /**
     * Returns the guild url.
     * 
     * @return string
     */
    public function getGuildUrl()
    {
        return $this->guildUrl;
    }
    
    /**
     * Returns the last modified date.
     * 
     * @return string
     */
    public function getLastModified()
    {
        return $this->lastModified;
    }
    
    /**
     * Returns the level.
     * 
     * @return string
     */
    public function getLevel()
    {
        return $this->level;
    }
    
    /**
     * Returns the name.
     * 
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    
    /**
     * Returns the points.
     * 
     * @return string
     */
    public function getPoints()
    {
        return $this->points;
    }
    
    /**
     * Returns the prefix.
     * 
     * @return string
     */
    public function getPrefix()
    {
        return $this->prefix;
    }
    
    /**
     * Returns the race.
     * 
     * @return string
     */
    public function getRace()
    {
        return $this->race;
    }
    
    /**
     * Returns the race id.
     * 
     * @return string
     */
    public function getRaceId()
    {
        return $this->raceId;
    }
    
    /**
     * Returns the realm.
     * 
     * @return string
     */
    public function getRealm()
    {
        return $this->realm;
    }
    
    /**
     * Returns the suffix.
     * 
     * @return string
     */
    public function getSuffix()
    {
        return $this->suffix;
    }
    
    /**
     * Returns the title id.
     * 
     * @return string
     */
    public function getTitleId()
    {
        return $this->titleId;
    }
    
    /**
     * Returns the arena teams.
     * 
     * @return array
     */
    public function getArenaTeams()
    {
        return $this->arenaTeams;
    }
    
    /**
     * Gets an arena team by type.
     * 
     * @param  string $type
     * @return Wow_Armory_Handler_Element_Default_Arena_Team
     */
    public function getArenaTeam($type)
    {
        if ($type != self::TWOONTWO
            && $type != self::THREEONTHREE
            && $type != self::FIVEONFIVE
        ) {
            return false;
        }
        
        return $this->_results['arenaTeams'][$type];
    }
        
}